﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using permit.App_Code.DataAccess;
using permit.App_Code.Entity;
using System.Data;

namespace permit.user
{
    public partial class UserManager : System.Web.UI.Page
    {
        UserAccess userAcc = new UserAccess();
        DepartmentAccess depAcc = new DepartmentAccess();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            btnDelete.Attributes.Add("onclick", "javascript:return(confirm('是否要删除该员工！'))");
            if (!Page.IsPostBack)
            {
                AssginAccess assAcc = new AssginAccess();
                UserTable userAu = (UserTable)Session["UserTable"];
                if (!assAcc.IsUserAssgin(userAu.USER_ID))
                {
                    Response.Redirect("~/main/Main.aspx?Sturt=User");
                }

                DataSet ds = depAcc.GetDepartments();

                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    if (dr["dep_AreaCode"].ToString() == userAu.DEP_AREACODE && userAu.DEP_AREACODE != "620100")
                    {
                        ddlDep.Items.Add(new ListItem(dr["dep_Name"].ToString(), dr["dep_ID"].ToString()));
                        break;
                    }
                    if (userAu.DEP_AREACODE == "620100")
                    {

                        ddlDep.Items.Add(new ListItem(dr["dep_Name"].ToString(), dr["dep_ID"].ToString()));
                    }

                }

                if (Request.QueryString["Sturt"] == "Edit")
                {

                    UserTable user = userAcc.getUserByID(Convert.ToInt32(Request.QueryString["user_ID"]));

                    hdName.Value = user.USER_NAME;
                    txtName.Text = user.NAME;
                    txtPosition.Text = user.USER_POSITION;
                    txtUserName.Text = user.USER_NAME;
                    Session["Password"] = user.USER_PASSWORD;
                    ddlDep.SelectedValue = user.DEP_ID.ToString();

                    btnAdd.Visible = false;
                    rvPassword.Visible = false;
                }
                if (Request.QueryString["Sturt"] == "Add")
                {

                    btnDelete.Visible = false;
                    btnEdit.Visible = false;
                    
                }
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
           
            if (txtPasswordOK.Text == txtPassword.Text)
            {
                if (txtUserName.Text != hdName.Value)
                {
                    if (userAcc.GetUser(txtUserName.Text))
                    {
                        Page.RegisterStartupScript("this1", "<script>alert('该用户名以存在！')</script>");
                        return;
                    }
                }

                if (!MeetingManagerLib.DataTypeCheck.IsDigitOrLetter(txtUserName.Text))
                {
                    Page.RegisterStartupScript("this2", "<script>alert('用户名不能包含中文字符！')</script>");
                    return;
                }
                if (!MeetingManagerLib.DataTypeCheck.IsDigitOrLetter(txtPassword.Text) && txtPassword.Text != "")
                {
                    Page.RegisterStartupScript("this3", "<script>alert('密码不能包含中文字符！')</script>");
                    return;
                }

                UserTable user = new UserTable();
                user.NAME = txtName.Text;
                user.USER_POSITION = txtPosition.Text;
                user.USER_NAME = txtUserName.Text;
                user.DEP_ID = Convert.ToInt32(ddlDep.SelectedValue);
                //没有填写密码，默认为原来密码
                if (txtPassword.Text != "")
                {
                    user.USER_PASSWORD = txtPassword.Text;
                }
                else
                {
                    user.USER_PASSWORD = Session["Password"].ToString();
                }

                user.USER_ID = Convert.ToInt32(Request.QueryString["user_ID"]);

                int count = userAcc.UpdateUser(user);
                if (count > 0)
                {

                    Response.Redirect(string.Format("UserInfo.aspx?depid={0}", user.DEP_ID));
                }
                else
                {
                    Page.RegisterStartupScript("this4", "<script>alert('修改失败！请查看网络是否连接！')</script>");
                }
            }
            else
            {
                Page.RegisterStartupScript("this5", "<script>alert('密码与确认密码不相同！')</script>");
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (txtPasswordOK.Text == txtPassword.Text.Trim())
            {
                if (userAcc.GetUser(txtUserName.Text))
                {
                    Page.RegisterStartupScript("this6", "<script>alert('该用户名以存在！')</script>");
                    return;
                }
                if (!MeetingManagerLib.DataTypeCheck.IsDigitOrLetter(txtUserName.Text))
                {
                    Page.RegisterStartupScript("this7", "<script>alert('用户名不能包含中文字符！')</script>");
                    return;
                }
                if (!MeetingManagerLib.DataTypeCheck.IsDigitOrLetter(txtPassword.Text))
                {
                    Page.RegisterStartupScript("this8", "<script>alert('密码不能包含中文字符！')</script>");
                    return;
                }
               
                UserTable user = new UserTable();
                user.NAME = txtName.Text;
                user.USER_POSITION = txtPosition.Text;
                user.USER_NAME = txtUserName.Text;
                user.DEP_ID = Convert.ToInt32(ddlDep.SelectedValue);
                user.USER_PASSWORD = txtPassword.Text;

                int count = userAcc.InsertUser(user);
                if (count > 0)
                {

                    Response.Redirect(string.Format("UserInfo.aspx?depid={0}", user.DEP_ID));
                }
                else
                {
                    Page.RegisterStartupScript("this9", "<script>alert('添加失败！请查看网络是否连接！')</script>");
                }
            }
            else
            {
                Page.RegisterStartupScript("this10", "<script>alert('密码与确认密码不相同！')</script>");
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int count = userAcc.DeleteUser(Convert.ToInt32(Request.QueryString["user_ID"]));

            if (count > 0)
            {
                Response.Redirect(string.Format("UserInfo.aspx?depid={0}", ddlDep.SelectedValue));
            }
            else
            {
                Page.RegisterStartupScript("this11", "<script>alert('删除数据失败！请查看网络是否连接！')</script>");
            }
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            txtName.Text = "";
            txtPosition.Text = "";
            txtUserName.Text = "";
            txtPassword.Text = "";
            txtPasswordOK.Text = "";
            ddlDep.SelectedIndex = 0;
        }


    }
}
